package com.noble.meether.chat.msgtype.base;

import com.noble.library.utils.JsonUtil;
import com.noble.meether.chat.MessageConstant;

import org.json.JSONObject;

/**
 * 消息类型处理
 */
public class BaseMessage implements IBaseMessage, Cloneable {
    // 自定义数据库字段
    private long id;                // 数据库：自增ID     排序使用
    private int isSender;           // 自己： 是否是消息发送者  = 1: 自己是发送者
    private long whisperId;         // 聊天对象Id
    private int sendStatus;         // 发送状态； 默认成功； = 1：表示发送失败

    // 消息体字段
    private long uidFrom;           // 发送者ID: uidFrom
    private long uidTo;             // 接收者ID:   uidTo
    private int msgType;            // 消息类型
    private String content;         // 消息内容
    private long time;              // 数据库：消息时间   排序使用(暂时未处理)

    private String jsonStr;         // 整个消息体
    private int Weight = MessageConstant.Small_Weight;               //Item的权重

    @Override
    public BaseMessage parseJson(String jsonStr) {
        this.setJsonStr(jsonStr);
        JSONObject jsonObject = JsonUtil.getJsonObject(jsonStr);
        this.uidFrom = jsonObject.optLong("UidFrom");
        this.uidTo = jsonObject.optLong("UidTo");
        this.msgType = jsonObject.optInt("TypeMsg");
        this.content = jsonObject.optString("Content");
        return this;
    }

    @Override
    public String getJson(String jsonContent) {
        this.setContent(jsonContent);
        return jsonContent;
    }

    /**
     * @return 自己: 是否是发送者
     */
    public boolean isSender() {
        return isSender == 1;
    }

    /**
     * @return 消息是否发送失败
     */
    public boolean isSendFail() {
        return sendStatus == 1;
    }

    public int getSendStatus() {
        return sendStatus;
    }

    public void setSendStatus(int sendStatus) {
        this.sendStatus = sendStatus;
    }

    public int getIsSender() {
        return isSender;
    }

    public void setIsSender(int isSender) {
        this.isSender = isSender;
    }

    public long getWhisperId() {
        return whisperId;
    }

    public void setWhisperId(long whisperId) {
        this.whisperId = whisperId;
    }

    public String getUidFrom() {
        return String.valueOf(uidFrom);
    }

    public long getLuidFrom() {
        return uidFrom;
    }

    public void setUidFrom(long uidFrom) {
        this.uidFrom = uidFrom;
    }

    public String getSuidTo() {
        return String.valueOf(uidTo);
    }

    public long getUidTo() {
        return uidTo;
    }

    public void setUidTo(long uidTo) {
        this.uidTo = uidTo;
    }

    public String getJsonStr() {
        return jsonStr;
    }

    public void setJsonStr(String jsonStr) {
        this.jsonStr = jsonStr;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getWeight() {
        return Weight;
    }

    public void setWeight(int weight) {
        Weight = weight;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public int getMsgType() {
        return msgType;
    }

    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public Object clone() {
        BaseMessage o = null;
        try {
            o = (BaseMessage) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

    public BaseMessage() {
        super();
    }

    @Override
    public String toString() {
        return "BaseMessage{" +
                "id=" + id +
                ", isSender=" + isSender +
                ", whisperId=" + whisperId +
                ", sendStatus=" + sendStatus +
                ", uidFrom=" + uidFrom +
                ", uidTo=" + uidTo +
                ", msgType=" + msgType +
                ", content='" + content + '\'' +
                ", time=" + time +
                ", jsonStr='" + jsonStr + '\'' +
                ", Weight=" + Weight +
                '}';
    }
}